//
//  ChooseCountViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/9.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "ChooseCountViewController.h"
#import "ChooseAcountCell.h"

@interface ChooseCountViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *_tableView;
    NSArray *_dataArr;
    ChooseAcountCell *_cell;
}
@end

@implementation ChooseCountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"选择账户";
    _dataArr = [NSArray array];
    self.view.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [self createTableView];
    [self downloadData];
    
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineChooseCount) Parameters:@{@"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        
        _dataArr = [responseObject[@"data"] allValues];
        
        NSMutableArray *muarr = [NSMutableArray array];
        
        for (id a in _dataArr) {
            if ([a isKindOfClass:[NSArray class]]) {
               
                [muarr addObject:a];
            }
        }
        if (muarr.count > 0) {
            _dataArr = muarr;
           [_tableView reloadData];
        }else{
            [self.view Message:@"数据为空"];
        }
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70)style:UITableViewStyleGrouped];
    //    space = [[QQSpaceView alloc] initWithFrame:_tableView.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"ChooseAcountCell" bundle:nil] forCellReuseIdentifier:@"ChooseAcountCell"];
    [_tableView setTableFooterView:[UIView new]];
    _tableView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:_tableView];
}
#pragma mark - UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataArr[section]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseAcountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseAcountCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = _dataArr[indexPath.section][indexPath.row];
    
    [cell cellConfig:dic];
//    cell.TypeName.text = dic[@"payName"];
//    if (self.isLineOnOrder) {
//        if ([dic[@"payCode"] isEqualToString:@"wallets"]) {
//            cell.BalanceLabel.attributedText = [self showLeft:dic[@"userMoney"]];
//        }else if ([dic[@"payCode"] isEqualToString:@"useraward"]) {
//            cell.BalanceLabel.attributedText = [self showLeft:dic[@"userAward"]];
//        }else{
//            cell.BalanceLabel.text = @"";
//        }
//    }else{
//        cell.BalanceLabel.attributedText = [self showLeft:_userMo];
//    }
//
//    __block LDPushOrderViewController *blockSelf = self;
//    cell.select = ^{
//        if (_cell) {
//            [_cell changeUi:YES];
//        }
//        _cell = cell;
//        _payType = dic[@"payCode"];
//        [blockSelf.PayBtn setBackgroundColor:[UIColor redColor]];
//        blockSelf.PayBtn.enabled = YES;
//    };
//    cell.remove = ^{
//        [blockSelf.PayBtn setBackgroundColor:[UIColor lightGrayColor]];
//        blockSelf.PayBtn.enabled = NO;
//    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UIView *point = [[UIView alloc]initWithFrame:CGRectMake(10, view.height/2-2.5, 5, 5)];
    point.backgroundColor = [UIColor redColor];
    [view addSubview:point];
    
    UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(point.right+10, 10, WIDTH-20, 30)];
    la.font = [UIFont systemFontOfSize:13];
    [view addSubview:la];
    
    NSDictionary *di = _dataArr[section][0];
    
    NSString *str;
    if ([di[@"accType"]integerValue]==1) {
        str = @"支付宝";
    }else if([di[@"accType"]integerValue]==2){
        str = @"微信";
    }else{
        str = @"银行卡";
    }
    la.text = str;
    
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cell) {
        _cell.choosed.selected = NO;
    }
    ChooseAcountCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    _cell = cell;
    cell.choosed.selected = YES;
    NSDictionary *dic = _dataArr[indexPath.section][indexPath.row];
    self.back(dic);
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
